package com.changgou.goods.controller;


import com.changgou.entity.PageResult;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.service.SpecService;
import com.changgou.pojo.Spec;
import com.github.pagehelper.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author gentleman Ma
 * @Date created in 2020/2/15
 * @description  规格控制器
 * @Version 1.0
 */
@RestController
@RequestMapping("/spec")
@Slf4j
public class SpecController {

    @Autowired
    private SpecService specService;


    /**
     * 根据商品分类名称查询规格列表
     * @param categoryName
     * @return
     */
    @GetMapping("/category/{categoryName}")
    public Result findListByCategoryName(@PathVariable("categoryName") String categoryName){

        List<Spec> specList = specService.findListByCategoryName(categoryName);
        return new Result(true, StatusCode.OK,"根据商品分类名称查询规格列表成功",specList);
    }


    /**
     * 查询全部数据
     * @return
     */
    @GetMapping("/findAll")
    public Result findAll(){
        List<Spec> specList = specService.findAll();
        return new Result(true,StatusCode.OK,"查询全部数据成功",specList);
    }


    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable("id") Integer id){
        Spec spec = specService.findById(id);
        return new Result(true,StatusCode.OK,"根据ID查询数据规格成功",spec);
    }


    /**
     * 新增数据
     * @param spec
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Spec spec){
        log.info("[新增规格数据]spec:{}",spec);
        specService.add(spec);
        return new Result(true,StatusCode.OK,"新增规格数据成功");
    }


    /**
     * 根据id修改数据
     * @param spec
     * @param id
     * @return
     */
    @PutMapping("/update/{id}")
    public Result update(@RequestBody Spec spec,@PathVariable("id") Integer id){
        log.info("[根据id修改规格数据]spec:{},id:{}",spec,id);
        spec.setId(id);
        specService.update(spec);
        return new Result(true,StatusCode.OK,"修改规格成功");
    }


    /**
     * 根据id删除规格数据
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Integer id){
        log.info("[根据id删除规格数据]id:{}",id);
        specService.delete(id);
        return new Result(true,StatusCode.OK,"根据id删除规格成功");

    }


    /**
     * 多条件搜索品牌数据
     * @param searchMap
     * @return
     */
    @GetMapping("/search")
    public Result findList(@RequestParam Map<String,Object> searchMap){
        log.info("[多条件搜索商品]searchMap:{}",searchMap);
        List<Spec> specList = specService.findList(searchMap);
        return new Result(true,StatusCode.OK,"多条件搜索成功",specList);
    }


    /**
     * 分页搜索查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/findPage/{page}/{size}")
    public Result findPage(@RequestParam Map<String,Object> searchMap, @PathVariable("page") int page,@PathVariable("size") int size){
        log.info("[分页搜索查询]searchMap:{},page:{},size:{}",searchMap,page,size);
        Page<Spec> specPage = specService.findPage(searchMap,page,size);
        PageResult pageResult = new PageResult(specPage.getTotal(),specPage.getResult());
        return new Result(true,StatusCode.OK,"分页查询成功",pageResult);
    }
}
